<?php

class SJB_Admin_Miscellaneous_UpdateToNewVersion extends SJB_Function
{
	public function isAccessible()
	{
		return true;
	}

	public function execute()
	{
		ini_set('max_execution_time', 0);
		$tp     = SJB_System::getTemplateProcessor();
		$errors = array();
		
		$isZipExtLoaded = extension_loaded('zip');
		$tp->assign('zip_extension_loaded', $isZipExtLoaded);
		
		$updatesDir  = SJB_System::getSystemSettings('SJB_UPDATES_DIR');
		$updateToVer = SJB_Request::getVar('to_version');
		if (!empty($updateToVer)) {
			// remove all path elements
			$updateToVer = basename($updateToVer);
			$startPath = $updatesDir . $updateToVer . DIRECTORY_SEPARATOR . "updater.php";

			if (file_exists($startPath)) {
				include_once $startPath;
			} else {
				echo '<p class="error">' . SJB_I18N::getInstance()->gettext(null, 'Update package files are missing') . '</p>';
			}
			$tp->display('updater_starts.tpl');
		} else {
			$formSubmitted = SJB_Request::getVar('update_to_version');
			$wayToUpdate = SJB_Request::getVar('way_to_update');

			if (!$isZipExtLoaded) {
				$errors[] = "The update process cannot be continued. There is no Zip-extension for PHP installed on your server.\nPlease install it and try again.";
			}

			if (!empty($formSubmitted) && $isZipExtLoaded) {
				// OK. we need to create Zend_Rest_Client, check user and get available updates if allowed
				$client = new Zend_Rest_Client(SJB_System::getSystemSettings('SJB_UPDATE_SERVER_URL'));

				$result = $client->getUpdateLink(
					SJB_Request::getVar('auth_username'),
					SJB_Request::getVar('auth_password'),
					SJB_System::getSystemSettings('version'))->get();

				if ($result->isSuccess()) {
					if (isset($result->error)) {
						$errors[] = (string) $result->error;
						$tp->assign("wayToUpdate", $wayToUpdate);
					}
					if (isset($result->update)) {
						$updateLink = (string) $result->update;
						$downloadedFileName = basename($updateLink);
						$unzipDirname       = basename($updateLink, '.zip');
						$downloadedFilePath = $updatesDir . DIRECTORY_SEPARATOR . $downloadedFileName;
						// download update file to cache/updates folder
						if ($wayToUpdate == 'makeArchive') {
							$this->sendArchiveToUser($updateLink,$downloadedFileName);
							exit();
						} elseif ($wayToUpdate == 'autoUpdate') {
							$result = copy($updateLink, $downloadedFilePath);
							if ($result) {
								if ($isZipExtLoaded) {
									$zip = new ZipArchive;
									$res = $zip->open($downloadedFilePath);
									if ($res === true) {
										$zip->extractTo($updatesDir . DIRECTORY_SEPARATOR . $unzipDirname . DIRECTORY_SEPARATOR);
										$zip->close();
										SJB_HelperFunctions::redirect(SJB_System::getSystemSettings('ADMIN_SITE_URL') . '/update-to-new-version/?to_version=' . urlencode($unzipDirname));
									} else {
										$errors[] = 'Failed to extract upgrade package files';
									}
								} else {
									$errors[] = "The update process cannot be continued. There is no Zip-extension for PHP installed on your server.\nPlease install it and try again.";
								}
							} else {
								$errors[] = 'Failed to download upgrade package';
							}
						}
					}
				}
			}
			$tp->assign('errors', $errors);
			$tp->display('update_to_new_version.tpl');
		}
	}

	/**
	 * @param string $updateLink
	 * @param string $downloadedFileName
	 */
	public function sendArchiveToUser($updateLink,$downloadedFileName)
	{
		for ($i = 0; $i < ob_get_level(); $i++) {
			ob_end_clean();
		}
		ini_set('max_execution_time', 0);
		header("Content-Type: application/octet-stream");
		header("Content-Disposition: attachment; filename=" . $downloadedFileName);

		readfile($updateLink);
	}

}
